package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.enums.PublishStatus;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Cyrus
 * @since 2020-12-08
 */
@Api(description = "课程服务")
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {



    @Autowired
    private EduCourseService courseService;

    //课程列表
    //TODO 完善条件分页
    @GetMapping("/getCourseList")
    public R getCourseList() {
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list",list);
    }

    @PostMapping("/getPageCourseList/{current}/{limit}")
    public R getPageCourseList(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> eduCoursePage = new Page(current, limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (courseQuery.getTitle() !=  null) {
            wrapper.lambda().eq(EduCourse::getTitle, courseQuery.getTitle());
        }
        if (courseQuery.getStatus() !=  null) {
            wrapper.lambda().eq(EduCourse::getStatus, courseQuery.getStatus());
        }
        if (courseQuery.getBegin() !=  null) {
            wrapper.lambda().ge(EduCourse::getGmtCreate, courseQuery.getBegin());
        }
        if (courseQuery.getTitle() !=  null) {
            wrapper.lambda().le(EduCourse::getGmtModified, courseQuery.getEnd());
        }
        wrapper.lambda().eq(EduCourse::getIsDeleted,0);
        IPage<EduCourse> page = courseService.page(eduCoursePage, wrapper);
        List<EduCourse> records = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("rows",records).data("total",total);
    }



    //删除课程
    @DeleteMapping("/deleteCourseById/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        courseService.deleteCourse(courseId);
        return R.ok();
    }



    //添加课程基本信息
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = courseService.addCourseInfo(courseInfoVo);
        //添加成功后返回课程id
        return R.ok().data("courseId",id);
    }

    //根据课程id查询基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        //添加成功后返回课程id
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //根据课程id查询基本信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = courseService.updateCourseInfo(courseInfoVo);
        //添加成功后返回课程id
        return R.ok().data("courseId",id);
    }

    //根据课程id查询课程确认信息
    @GetMapping("/getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId) {
        CoursePublishVo coursePublishVo=courseService.PublishCourseInfo(courseId);
        return R.ok().data("coursePublishVo",coursePublishVo);
    }


    //课程最终发布
    @PostMapping("/publish/{courseId}")
    public R publish(@PathVariable String courseId) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus(PublishStatus.NORMAL.getStatus());
        courseService.updateById(eduCourse);
        return R.ok();
    }
}

